package xm.components.table
{
	import flash.display.Graphics;
	import flash.display.Shape;
	
	import xm.core.XMSprite;
	
	
	/**
	 * <br> 클래스 설명 : 트랜지션이 없고, XMSprite만 직접 상속받은 테이블.</br>
	 * @author 김정환
	 */	
	public class Table extends XMSprite
	{
		
		//-----------------------------------------------------------------------------------
		// public vars
		//-----------------------------------------------------------------------------------
		
		
		public var trList : Vector.<TR> = new Vector.<TR>();
		
		
		//-----------------------------------------------------------------------------------
		// protected / internal vars
		//-----------------------------------------------------------------------------------
		
		
		//-----------------------------------------------------------------------------------
		// private vars
		//-----------------------------------------------------------------------------------
		
		
		
		
		//-----------------------------------------------------------------------------------
		// public functions
		//-----------------------------------------------------------------------------------
		
		/**
		 * constructor 
		 */	
		public function Table()
		{
			super();
		}
		
		
		
		/**
		 * commitProperties
		 */		
		override public function commitProperties():void
		{
			super.commitProperties();
			
		}
		
		
		
		/**
		 * updateDisplayList
		 */		
		override public function updateDisplayList( enforce : Boolean = false ):void
		{
			super.updateDisplayList(enforce);
			
			
			// drawBg 재정의를 통해 bg 및 borderLine 등을 정의하고, 
			// tr, td 의 속성등도 함께 정의한다.
			drawBg();
			
		}
		
		
		/**
		 * tr 삽입.
		 * @param tr : TR
		 */		
		public function addTR( tr:TR ):void
		{
			this.trList.push( tr );
		}
		
		/**
		 * @param tr
		 * @param index
		 */		
		public function addTRAt( tr:TR, index:int ):void
		{
			if( trList.length > index )
			{
				trList.splice( index, 0, tr ); 
			}
		}
		
		/**
		 * @param tr : TR - 제거.
		 */		
		public function removeTR( tr:TR ):void
		{
			if( trList.indexOf( tr ) > -1 )
			{
				trList.splice( trList.indexOf( tr ) , 1 );
			}
		}
		
		/**
		 * @param tr : TR - 제거.
		 */		
		public function removeTRAt( index:int ):void
		{
			if( trList.length > index )
			{
				trList.splice( index , 1 );
			}
		}
		
		
		
		
		
		
		
		/**
		 * @param height - -1 은 너비 자동셋팅.
		 * @param borderBottomAlpha
		 * @param borderBottomColor
		 * @param borderBottomThickness
		 * @param autoAdd
		 * @return : TR
		 */		
		public function makeTR( height : Number,
								backgroundColor : uint = 0xffffff,
								backgroundAlpha : Number = 0,
								borderTopThickness : Number = 1,
							    borderTopColor : Number = 0xcccccc,
							    borderTopAlpha : Number = 1,
								autoAdd : Boolean = true ):TR
		{
			var tr : TR;
			
			tr = new TR();
			tr.height = height;
			tr.backgroundColor = backgroundColor;
			tr.backgroundAlpha = backgroundAlpha;
			tr.borderTopThickness = borderTopThickness;
			tr.borderTopColor = borderTopColor;
			tr.borderTopAlpha = borderTopAlpha;
			if( autoAdd ) this.addTR( tr );
			
			return tr;
		}
		
		
		
		
		
		
		
		//-----------------------------------------------------------------------------------
		// life cycle functions
		//-----------------------------------------------------------------------------------
		
		/**
		 * createChildren
		 */		
		override public function createChildren():void
		{
			super.createChildren();
			
			bg = new Shape();
			addChild( bg );
		}
		
		
		/**
		 * createComplete
		 */		
		override public function creationComplete():void
		{
			super.creationComplete();
			
		}
		
		
		/**
		 * add
		 */		
		override public function add():void
		{
			super.add();
		}
		
		
		/**
		 * remove
		 */		
		override public function remove():void
		{
			super.remove();
		}
		
		
		/**
		 * destroy
		 */		
		override public function destroy():void
		{
			
			var i:int, len : int;
			
			
			i=0;
			len = trList.length;
			
			while( i<len )
			{
				trList[ i ].destroy();
				delete trList[ i ];
				++i;
			}
			trList = null;
			
			
			super.destroy();
			
		}
		
		//-----------------------------------------------------------------------------------
		// setter / getter
		//-----------------------------------------------------------------------------------
		
		
		
		//-----------------------------------------------------------------------------------
		// protected functions
		//-----------------------------------------------------------------------------------
		
		/**
		 * <br>bg 그리는 메서드 - 재정의</br>
		 * <br>tr, td 의 포지션, 사이즈, 라인, 배경색, td.updateDisplayList() 호출 등을 모두실행</br>
		 * <br>table의 key함수.</br>
		 * <br>상속 및 확장시 반드시 이 함수를 재정의 하세요.</br>
		 */		
		override protected function drawBg():void
		{
			
			var g : Graphics = bg.graphics,
				i : int = 0,
				len : int = trList.length,
				tr : TR,
				td : TD,
				j : int = 0,
				jLen : int = 0;
			
			
			g.clear();
			
			
			/////////////////////////배경 그리기 ( 재정의됨 ) ///////////////////////////////
			if( backgroundAlpha != 0 )
			{
				g.beginFill( backgroundColor, backgroundAlpha );
				g.drawRect( paddingLeft, paddingTop, 
					Math.max( viewWidth - paddingLeft - paddingRight, 0 ), 
					Math.max( viewHeight - paddingTop - paddingBottom, 0 ) );
				g.endFill();
			}
			
			
			
			/////////// td 설정.
			i=0;
			while( i<len )
			{
				tr = trList[ i ];
				
				tr.x = 0;
				tr.width = viewWidth;
				if( i == 0 ) tr.y = 0;
				else tr.y = trList[ i - 1 ].y + trList[ i - 1 ].height;
				
				// 그래픽 라인 초기화.
				g.lineStyle();
				if( tr.backgroundAlpha != 0 )
				{
					g.beginFill( tr.backgroundColor, tr.backgroundAlpha );
					g.drawRect( tr.x+1, tr.y+1, tr.width-1, tr.height-1 );
					g.endFill();
				}
				
				if( tr.borderTopThickness != 0 )
				{
					g.lineStyle( tr.borderTopThickness, tr.borderTopColor, tr.borderTopAlpha );
					g.moveTo( 1, tr.y );
					g.lineTo( viewWidth, tr.y );
				}
				
				
				
				
				
				
				///////////// tr 설정.
				j=0;
				jLen = tr.tdList.length;
				
				while( j<jLen )
				{
					td = tr.tdList[ j ];
					td.parentTable = this;
					
					td.y = tr.y;
					td.height = tr.height;
					if( j==0 ) td.x = 0;
					else td.x = tr.tdList[ j-1 ].x + tr.tdList[ j-1 ].width;
					td.updateDisplayList();
					
					// 그래픽 라인 초기화.
					g.lineStyle();
					if( td.backgroundAlpha != 0 )
					{
						g.beginFill( td.backgroundColor, td.backgroundAlpha );
						g.drawRect( td.x + 1, 
							td.y + 1, 
							td.width-1, 
							td.height-1 );
						g.endFill();
					}
					
					
					if( td.borderLeftThickness != 0 )
					{
						g.lineStyle( td.borderLeftThickness, td.borderLeftColor, td.borderLeftAlpha );
						g.moveTo( td.x, tr.y+1 );
						g.lineTo( td.x, tr.y+tr.height );
					}
					
					
					++j;
				}
				
				++i;
			}
			
			
			/////////////////////////// 외곽 그리기 ( 재정의됨 ) ///////////////////////////////
			// 상단 보더
			if( borderTopThickness != 0 )
			{
				g.lineStyle( borderTopThickness, borderTopColor, borderTopAlpha );
				g.moveTo( 0, 0 );
				g.lineTo( viewWidth, 0 );
			}
			
			// 우측 보더
			if( borderRightThickness != 0 )
			{
				g.lineStyle( borderRightThickness, borderRightColor, borderRightAlpha );
				g.moveTo( viewWidth, 0 );
				g.lineTo( viewWidth, viewHeight );
			}
			
			// 하단 보더
			if( borderBottomThickness != 0 )
			{
				g.lineStyle( borderBottomThickness, borderBottomColor, borderBottomAlpha );
				g.moveTo( viewWidth, viewHeight );
				g.lineTo( 0, viewHeight );
			}
			
			// 좌측 보더
			if( borderLeftThickness != 0 )
			{
				g.lineStyle( borderLeftThickness, borderLeftColor, borderLeftAlpha );
				g.moveTo( 0, viewHeight );
				g.lineTo( 0, 0 );
			}
		}
		
		
		//-----------------------------------------------------------------------------------
		// internal functions
		//-----------------------------------------------------------------------------------
		
		
		
		
		//-----------------------------------------------------------------------------------
		// private functions
		//-----------------------------------------------------------------------------------
		
		
		
		//-----------------------------------------------------------------------------------
		// event handler
		//-----------------------------------------------------------------------------------
		
		
		
		
		
		
		
		
		
	}
}